package com.icas.controller;

import com.alibaba.fastjson.JSONObject;
import com.icas.util.Constant;
import com.icas.util.RoleConstant;
import com.icas.util.jwt.JwtUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * @author tocchen
 * @date  2022-04-22-17:10
 * @deprecated 请求转发的具体实现
 */

@RestController
@RequestMapping("/kingzhihui/face")
@RequiresAuthentication
public class RestTemplateController {

    @PostMapping("/information")
    public JSONObject restTemplate(@RequestParam(value = "faceInformation") String faceInformation, @RequestHeader(value = "token") String token){

        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        HttpMethod method = HttpMethod.POST;


        ResponseEntity<JSONObject> exchange = null;

        MultiValueMap<String, String> valueMap = new LinkedMultiValueMap<>();
        valueMap.add("faceInformation",faceInformation);
        if (RoleConstant.STUDENT_ROLE.equals(JwtUtils.getRole(token))){
            headers.add("token", Constant.STU_FACE_SERVICE_TOKEN);
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            valueMap.add("userId",JwtUtils.getAccount(token));
            HttpEntity<MultiValueMap<String,String>> requestEntity = new HttpEntity<>(valueMap, headers);
            exchange = client.exchange(Constant.STU_FACE_URL, method, requestEntity, JSONObject.class);
        }else {
            headers.add("token", Constant.FACE_SERVICE_TOKEN);
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity<MultiValueMap<String,String>> requestEntity = new HttpEntity<>(valueMap, headers);
            exchange = client.exchange(Constant.ADMIN_FACE_URL, method, requestEntity, JSONObject.class);
        }

        return exchange.getBody();

    }

}
